const wxParse = require('../../templates/wxParse/wxParse');

Page({
  data: {
    recharge: null,
    selectedIdx: null,
    tabs: [
      {name: '充值', tab: 'recharge'},
      {name: '记录', tab: 'logs'},
      //{name: '消费记录', tab: 'paymentLogs', pageIndex: 1, noMore: false}
    ],
    currentTab: 'recharge',
    pageIndex: 1,
    noMore: false,
    logs: []
  },
  onLoad: function () {
    let height = 0, sys = wx.getSystemInfoSync(), config = wx.getStorageSync('rf_config');
    if (sys.windowHeight < 750) {
      height = 710;
    } else {
      height = 960;
    }
    this.setData({
      height: height,
      popupHeight: sys.windowHeight - 50,
      company: config.copyright_companyname
    });
    wxParse.wxParse('article', 'html', config.protocol_recharge, this, 5);
  },
  onShow: function () {
    this.setData({
      //balance: wx.getStorageSync('rf_member')['account']['user_money']
      account: wx.getStorageSync('rf_member')['account']
    });
    // 充值金额列表
    wx.util.request('user.recharge').then(recharge => {
      this.setData({
        recharge: recharge
      });
    });
    // 变动记录
    this.getLogs();
  },
  onPageScroll: function (e) {
    this.setData({
      scrollTop: e.scrollTop
    });
  },
  getLogs: function () {
    if (this.data.noMore) return;
    wx.util.request('user.statement', {
      query: `credit_type=1&page=${this.data.pageIndex}`
    }).then(logs => {
      for (let item of logs) {
        item.date = wx.time.toDate(item.updated_at);
      }
      this.setData({
        logs: this.data.logs.concat(logs),
        pageIndex: this.data.pageIndex + 1,
        noMore: logs.length < 10
      });
    });
  },
  selected: function(e) {
    let idx = e.currentTarget.dataset.index;
    this.setData({
      selectedIdx: (idx == this.data.selectedIdx ? null : idx)
    });
  },
  protocol: function (e) {
    this.setData({
      agreement: e.detail.value[0] == 1
    });
  },
  recPro: function () {
    this.setData({
      popupShow: true
    });
  },
  hidePopup: function () {
    this.setData({
      popupShow: false
    });
  },
  recharge: function () {
    wx.navigateTo({
      url: '../payment/index?type=recharge&money=' + this.data.recharge[this.data.selectedIdx].price,
    });
  },
  switch: function (e) {
    this.setData({
      currentTab: e.currentTarget.dataset.tab
    });
  },
  more: function () {
    this.getLogs();
  }
})